package com.nulldev.util.web.HttpServer3.extensions.envStats;

import com.nulldev.util.languages.json.uniapi.UniJsonValue;
import com.nulldev.util.manage.nullUtil;
import com.nulldev.util.web.HttpClient.HttpClient;
import com.nulldev.util.web.HttpClient.providers.HttpClientProvider;
import com.nulldev.util.web.HttpServer3.Response;
import com.nulldev.util.web.HttpServer3.pages.v3.HttpPage3;

public class HTTPStatsPage extends HttpPage3 {

	private static final String JSON_MIME = "application/json; charset=utf-8";

	public HTTPStatsPage() {
		super("/debug/http");
	}

	@SuppressWarnings("resource")
	@Override
	public Response onRequest(final Request request) {
		if (!nullUtil.IS_NON_MAINLINE_BUILD)
			return new Response(204, BLANK_IO_STREAM);
		final UniJsonValue uv = UniJsonValue.object(3);
		uv.add("VERSION", HttpClient.VERSION);
		{
			final UniJsonValue standard = UniJsonValue.object(4);
			final HttpClientProvider stdProvider = HttpClient.getStandardBackend();
			standard.add("string", stdProvider.toString());
			standard.add("caching", stdProvider.defaultCachingType());
			standard.add("id", stdProvider.getID());
			final UniJsonValue ff = UniJsonValue.object(2);
			ff.add("supportsRequests", stdProvider.providesRequests());
			ff.add("supportsWebSockets", stdProvider.providesWebSockets());
			standard.add("features", ff);
			uv.add("standard", standard);
		}
		{
			final UniJsonValue legacy = UniJsonValue.object(4);
			final HttpClientProvider legacyProvider = HttpClient.getLegacyBackend();
			legacy.add("string", legacyProvider.toString());
			legacy.add("caching", legacyProvider.defaultCachingType());
			legacy.add("id", legacyProvider.getID());
			final UniJsonValue ff = UniJsonValue.object(2);
			ff.add("supportsRequests", legacyProvider.providesRequests());
			ff.add("supportsWebSockets", legacyProvider.providesWebSockets());
			legacy.add("features", ff);
			uv.add("legacy", legacy);
		}
		return new Response(200, uv, uv).setCustomMime(JSON_MIME);
	}

}
